package com.cwj.fron.customer.aop;

import com.cwj.common.entity.customer;
import com.cwj.common.entity.customer_log;
import com.cwj.common.utils.IpUtil;
import com.cwj.common.utils.Md5Utils;
import com.cwj.common.utils.RedisUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.data.annotation.Transient;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Aspect
@Component
public class registerLoginAop {
    @Resource
    com.cwj.common.dao.customer_logMapper customer_logMapper;
    @Resource
    private HttpServletRequest request;
    @Pointcut("execution (* com.cwj.fron.customer.service.registerLogin.customer_login(..))")
    public void customer_login() {
    }
    @Resource
    RedisUtil redisUtil;
    @Resource
    ObjectMapper objectMapper;
    @AfterReturning("customer_login()")
    public void login() throws JsonProcessingException {
        customer customer = (com.cwj.common.entity.customer) request.getAttribute("customer");
        customer_log customer_log = new customer_log();
        customer_log.setCustomer_id((customer.getId()));
        customer_log.setOperation_time(new Date());
        customer_log.setType(com.cwj.common.entity.customer_log.operation.login.toString());
        customer_log.setIp(IpUtil.getIp(request));
        String token=Md5Utils.code(objectMapper.writeValueAsString(customer))+
                new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(customer_log.getOperation_time());
        customer_log.setToken(token);
//        customer_logMapper.insert(customer_log);
        redisUtil.set("customerId:"+customer.getId(),token);
        System.out.println(token);
        request.setAttribute("customer_log",customer_log);
    }

}
